Ext.namespace('Ext.ux');


/**
 * 单选 Tree
 */

Ext.ux.SingleCheckTree = function(config) {
	this.config = config;
   	Ext.ux.SingleCheckTree.superclass.constructor.call(this,config);
	this.on('checkchange',this.checkchange);
	
}

Ext.extend(Ext.ux.SingleCheckTree,Ext.tree.TreePanel,{
	
	width : "100%",
	
	height : 350,
	
	useArrows : false,
	
	autoScroll : true,
	
	animate : true,
	
	enableDD : false,
	
	containerScroll : true,
	
	rootVisible : false,
	
	
	evt : {},
	
	frame : true,
	
	
	getSelNodes  : function() {
		var sleNodeId = "";
		var selNodes = this.getChecked();
		Ext.each(selNodes, function(node){
			if (node.id != "root") {
				if (sleNodeId == "") {
					sleNodeId += node.attributes.columncode;
				} else {
					sleNodeId += ","+node.attributes.columncode;
				}
			}
		});
		
		return sleNodeId;
	},
	
	checkchange : function(node, checked) {
		var nodes = node.parentNode.childNodes;
		if (checked) {
			node.getUI().addClass('complete');
			if(node.parentNode) {
				node.parentNode.ui.toggleCheck(checked);
				node.parentNode.attributes.checked = checked;
			}
			for(var i=0;i<nodes.length; i++) {
				if (nodes[i].attributes.checked && nodes[i].id != node.id) {
					nodes[i].ui.toggleCheck(!checked);
					nodes[i].attributes.checked = !checked;
					nodes[i].eachChild(function(child) {
						if(child.attributes.checked) {
							child.ui.toggleCheck(!checked);
							child.attributes.checked = !checked;
						}
					})
				}
			}
		} else {
			node.getUI().removeClass('complete');
			var n = 0;
			for(var i=0;i<nodes.length; i++) {
				if (nodes[i].attributes.checked) {
					n ++;
					break;
				}
			}
			if(node.parentNode  &&  n == 0) {
				node.parentNode.ui.toggleCheck(checked);
				node.parentNode.attributes.checked = checked;
				node.eachChild(function(child) {
					if(child.attributes.checked) {
						child.ui.toggleCheck(checked);
						child.attributes.checked = checked;
					}
				})
			}
		};
		node.expand();
		node.attributes.checked = checked;
	}
	
	/*				
	tools : [{
			id : 'refresh',
			qtip : '刷新',
			handler : function() {
				Ext.getCmp('single-check-tree').getRootNode().reload();
			}
	}]	*/
});

Ext.reg('singlechecktree', Ext.ux.SingleCheckTree);


Ext.namespace('VAS');
VAS.SingleCheckTreeWin = function($config) {
	var _this = this;
	var tree = new Ext.ux.SingleCheckTree({
		root : {
			nodeType : 'async',
			text : "root",
			id   : "root"
		},
		dataUrl : $config.url,
		evt 	: $config.evt,
		border  : false,
		height 	: $config.height - 70
	});
	
	
	

	var thisConfig = {
		items : [tree,{
			buttons : [{
				text  : '确定',
				width :  40,
				handler: function(){
					if (tree.getSelNodes() == "") {
							Ext.Msg.alert('提示',"请选择数据");
					} else {
						tree.evt.setValue(tree.getSelNodes());
						_this.close();
						
					}
				}
			},{
				text : '关闭',
				width :  40,
				handler: function(){
					_this.close();
				}
			}]
		}]
	};

	var _config = $config || {};

	Ext.apply(_this, Ext.apply(thisConfig, _config));
	VAS.SingleCheckTreeWin.superclass.constructor.call(_this);

};

Ext.extend(VAS.SingleCheckTreeWin, VAS.BasicWindow, {});
Ext.reg('singlechecktree-win', VAS.SingleCheckTreeWin);